﻿using Microsoft.AspNetCore.Mvc;

using PLCS.Domain.Logs;
using PLCS.SocialHttpClient.WMS.Models;

using Volo.Abp.DependencyInjection;

namespace PLCS.SocialHttpClient.AGV;

public class AGVSocialService : ITransientDependency
{
    private readonly ApiRequestService _requestService;

    public AGVSocialService(ApiRequestService requestService)
    {
        _requestService = requestService;
    }

    public async Task<AGVResponse> ReleaseTaskAsync(string taskId, string equipmentNo, string startPos, string endPos, int needStop = 0, int level = 0)
    {
        var result = await _requestService.SendWebApiRequest<AGVResponse>(PLCSSociaHttpClientConstant.plcsToAgvTaskReleaseUrl, HttpMethod.Post, "AGV", "PLCS下发AGV任务", "PLCS下发AGV任务", new
        {
            header = new
            {
                app = "wcs",
                code = "plcs_to_agv_task_release",
                seqNo = Guid.NewGuid().ToString(),
            },
            body = new
            {
                taskId = taskId,
                equipmentNo = equipmentNo,
                startPos = startPos,
                endPos = endPos,
                needStop = needStop,
                level = level
            }
        });

        return result;
    }

    public async Task<GetAgvsStatusRepose> GetEquipmentsStatusAsync()
    {
        GetAgvsStatusRepose result;
        string seqNo = Guid.NewGuid().ToString();
        try
        {
            result = await _requestService.SendWebApiRequest<GetAgvsStatusRepose>(PLCSSociaHttpClientConstant.plcsToPlcsAllEquipmentStatusUrl, HttpMethod.Post, "AGV", "PLCS获取AGV状态", "PLCS获取AGV状态", new
            {
                header = new
                {
                    app = "wcs",
                    code = "plcs_to_plcs_allEquipment_status",
                    seqNo = seqNo,
                },
                body = new
                {
                }
            });
        }
        catch (Exception ex)
        {
            result = new GetAgvsStatusRepose()
            {
                Header = new Header()
                {
                    Code = "E",
                    Message = ex.Message,
                    SeqNo = seqNo
                }
            };
        }

        return result;
    }

    public async Task<AGVResponse> CancelTaskAsync(string taskId, string equipmentNo)
    {
        AGVResponse result;
        string seqNo = Guid.NewGuid().ToString();
        try
        {
            result = await _requestService.SendWebApiRequest<AGVResponse>(PLCSSociaHttpClientConstant.plcsToAgvCancelTaskUrl, HttpMethod.Post, "AGV", "PLCS下发AGV任务", "PLCS下发AGV任务", new
            {
                header = new
                {
                    app = "wcs",
                    code = "plcs_to_agv_cancel_task",
                    seqNo = seqNo,
                },
                body = new
                {
                    taskId = taskId,
                    equipmentNo = equipmentNo
                }
            });
        }
        catch (Exception ex)
        {
            result = new AGVResponse()
            {
                Header = new Header()
                {
                    Code = "E",
                    Message = ex.Message,
                    SeqNo = seqNo
                }
            };
        }

        return result;
    }
}